package mrunit;

/**
 * Created by 赵攀 on 2017/7/18.
 */

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.ql.io.RCFileInputFormat;
import org.apache.hadoop.hive.serde2.columnar.BytesRefArrayWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.util.GenericOptionsParser;

import java.io.IOException;
import java.util.Iterator;

public class RcFileReaderJob {
    static class RcFileMapper implements Mapper<LongWritable, BytesRefArrayWritable, Text, NullWritable> {
        private Text word =new Text();
        @Override
        public void map(LongWritable key, BytesRefArrayWritable value,
                           OutputCollector<Text, NullWritable> output, Reporter reporter)throws IOException {
            Text txt = new Text();
            txt.set(value.get(0).getData(), value.get(0).getStart(), value.get(0).getLength());
            String[] result = txt.toString().split(",");
            for(int i=0; i < result.length; i++){
                word.set(result[i]);
                output.collect(word, NullWritable.get());
            }

        }


        @Override
        public void close() throws IOException {

        }

        @Override
        public void configure(JobConf job) {

        }
    }

    static class RcFileReduce implements Reducer<Text, NullWritable, Text, NullWritable> {
        @Override
        public void reduce(Text key, Iterator<NullWritable> value,
                           OutputCollector<Text, NullWritable> output, Reporter reporter) throws IOException {
            output.collect(key, NullWritable.get());
        }

        @Override
        public void close() throws IOException {

        }

        @Override
        public void configure(JobConf job) {

        }
    }

    public static void runLoadMapReducue(JobConf conf, Path input, Path output) throws IOException,
            ClassNotFoundException, InterruptedException {
/*
        Job job = Job.getInstance(conf);
        job.setJarByClass(RcFileReaderJob.class);
        job.setJobName("RcFileReaderJob");
        job.setNumReduceTasks(1);
        job.setMapperClass(RcFileMapper.class);
        job.setReducerClass(RcFileReduce.class);
        job.setInputFormatClass(RCFileMapReduceInputFormat.class);
        //MultipleInputs.addInputPath(job, input, RCFileInputFormat.class);
        RCFileMapReduceInputFormat.addInputPath(job, input);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);
        FileOutputFormat.setOutputPath(job, output);
        return job.waitForCompletion(true);
        */

        conf.setJobName("WordCountRC");

        conf.setOutputKeyClass(Text.class);
        conf.setOutputValueClass(NullWritable.class);

        conf.setMapperClass(RcFileMapper.class);
        //conf.setCombinerClass(Reduce.class);
        conf.setReducerClass(RcFileReduce.class);

        conf.setInputFormat(RCFileInputFormat.class);
        conf.setOutputFormat(TextOutputFormat.class);

        FileInputFormat.setInputPaths(conf, input);
        FileOutputFormat.setOutputPath(conf, output);

        JobClient.runJob(conf);
    }

    public static void main(String[] args) throws Exception {
        JobConf conf = new JobConf(RcFileReaderJob.class);
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
        for(String s:otherArgs){
            System.out.println(s);
        }
        if (otherArgs.length != 2) {
            System.err.println("Usage: rcfile <in> <out>");
            System.exit(2);
        }
        RcFileReaderJob.runLoadMapReducue(conf, new Path(otherArgs[0]), new Path(otherArgs[1]));
    }
}
